<?php
/**
 * autoload file 
 * @param string $prefixPath
 * @param string $name
 * @param string $postfix
 */
function includeFile($prefixPath,$name,$postfix = '.php') {
    $_file = isFileExists($prefixPath, $name, $postfix);
	if (!empty($_file)) {
		require_once $_file;
	} else {
	    if (DEV_ENV) {
		    $tmp = debug_backtrace();
		    trigger_error(Common::recursiveImplode("\n",$tmp),E_USER_WARNING);
		    Message::showMessage('__autoload class file '.addslashes($name).' not found');
	    }
	    else {
	        header("Location: /");
	    }
	}
}
/**
 * check the include file exists
 * @param string $prefixPath
 * @param string $name
 * @param string $postfix
 * @return file full path or false on failure
 */
function isFileExists($prefixPath, $name, $postfix = '.php') {
	$name = str_replace('.', '', $name);   //security check file name
	$_file = $prefixPath.$name.$postfix;
	if (file_exists($_file)) {
		return $_file;
	}
	return false;
}
/**
 * autoload class's name
 * @param string $name
 */
function __autoload($name) {
	if (stripos($name,FOLDER_CONFIG) !== FALSE) {
		includeFile(CONFIG_PATH,$name);
	}
	else if (stripos($name,'base') !== FALSE) {//base model
		 if (stripos($name,FOLDER_CTL) !== FALSE) {
			includeFile(LIBS_CTL_PATH,$name);
		 }
		 else if (stripos($name,FOLDER_MODEL) !== FALSE ) {
		 	includeFile(LIBS_MODEL_PATH,$name);
		 }
		 else if (stripos($name,FOLDER_VIEW) !== FALSE ) {
		 	includeFile(LIBS_VIEW_PATH,$name);
		 }else{}
	}
	else if (stripos($name,'smarty') !== FALSE) {
		includeFile(LIBS_VIEW_PATH,$name,'.class.php');
	}
	else if (stripos($name,FOLDER_CTL) !== FALSE) {//www controller
		includeFile(CTL_PATH,$name);
	}
	else if (stripos($name, FOLDER_SERVICE) !== FALSE) {
	    includeFile(SERVICE_PATH, $name);
	}
	else{//OO-Model	
		if (isFileExists(LOGIC_MODEL_DB_PATH, $name)) {
			includeFile(LOGIC_MODEL_DB_PATH, $name);
		}
		else if (isFileExists(LOGIC_MODEL_CACHE_PATH, $name)) {
			includeFile(LOGIC_MODEL_CACHE_PATH, $name);
		}
		else {
			includeFile(MODEL_PATH,$name);
		}
	}
}
?>